package sc;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Tray {

	private static SystemTray tray;
	private static MenuItem showHide;
	private static Image baseimage;
	private static TrayIcon trayicon;
	private static MenuItem openForum;
	private static MenuItem joinChat;
	private static MenuItem joinVent;
	
	public static void initialise()
	{
		if (!SystemTray.isSupported())
			return;

		tray = SystemTray.getSystemTray();

		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String cmd = e.getActionCommand();
				if (cmd == "Show Member List")
				{
					Harlequin.getInstance().setVisible(true);
					showHide.setLabel("Hide Member List");
				}
				else if (cmd == "Hide Member List")
				{
					Harlequin.getInstance().setVisible(false);
					showHide.setLabel("Show Member List");
				}
				else if (cmd == "Open Forum")
				{
					OpenURL.openForum();
				}
				else if (cmd == "Join Chat")
				{
					OpenURL.joinChat();
				}
				else if (cmd == "Join Vent")
				{
					OpenURL.joinVent();
				}
				else if (cmd == "Configuration...")
				{
					Configuration.open();
				}
				else if (cmd == "Exit Harlequin")
					System.exit(0);
			}
		};

		
		PopupMenu popup = new PopupMenu();
		showHide = new MenuItem("Show Member List");
		showHide.addActionListener(listener);
		popup.add(showHide);
				
		openForum = new MenuItem("Open Forum");
		openForum.addActionListener(listener);
		popup.add(openForum);
		
		joinChat = new MenuItem("Join Chat");
		joinChat.addActionListener(listener);
		popup.add(joinChat);
		
		joinVent = new MenuItem("Join Vent");
		joinVent.addActionListener(listener);
		popup.add(joinVent);

		MenuItem item = new MenuItem("Configuration...");
		item.addActionListener(listener);
		popup.add(item);
		popup.addSeparator();
		item = new MenuItem("Exit Harlequin");
		item.addActionListener(listener);
		popup.add(item);

		baseimage = Images.getImage("/images/harlequin.png");
		trayicon = new TrayIcon(baseimage, Configuration.properties.getProperty("client.title"),
				popup);

		trayicon.setImageAutoSize(true);
		trayicon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String cmd = e.getActionCommand();
				if (cmd == "OK")
					return;
				showHideItem(!Harlequin.getInstance().isVisible());
				Harlequin.getInstance().setVisible(!Harlequin.getInstance().isVisible());
			}
		});
		

		enableOpenForum(false);		
		enableJoinChat(false);
		enableJoinVent(false);

		try
		{
			tray.add(trayicon);
		}
		catch (AWTException e)
		{
			Log.info("TrayIcon could not be added.");
		}
	}
	
	public static void showHideItem(boolean hide)
	{
		if (hide)
			showHide.setLabel("Hide Member List");
		else
			showHide.setLabel("Show Member List");
	}
	
	public static void event(String msg)
	{
		if (Member.userMember() == null)
			return;
		Log.info(msg);
		trayicon.displayMessage(Harlequin.CLIENT_TITLE, msg, TrayIcon.MessageType.INFO);
	}
	
	public static void enableOpenForum(boolean enable)
	{
		openForum.setEnabled(enable);	
	}
	
	public static void enableJoinChat(boolean enable)
	{
		joinChat.setEnabled(enable);	
	}
	
	public static void enableJoinVent(boolean enable)
	{
		joinVent.setEnabled(enable);	
	}
	
	public static void setToolTip(String tooltip)
	{
		trayicon.setToolTip(tooltip);
	}
}
